package com.qire.manhua.controller;

import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;
import com.qire.manhua.R;
import com.qire.manhua.activity.PasswordChangeGuideActivity;
import com.qire.manhua.databinding.ActivityPasswordChangeGuideBinding;
import com.qire.manhua.model.enumeration.AccountBusinessPanelLayout;
import com.qire.manhua.model.viewModel.impl.PasswordChangeGuideViewModel;

import androidx.annotation.NonNull;

public class PasswordChangeGuideController extends EventController<PasswordChangeGuideActivity> {

    private PasswordChangeGuideViewModel viewModel;

    public PasswordChangeGuideController(@NonNull PasswordChangeGuideActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityPasswordChangeGuideBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setPasswordChangeGuideController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(PasswordChangeGuideViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    /**退出按钮事件响应*/
    public void backBtnOnClick(View view) {
        owner.finish();
    }

    public void oldPasswordVerifyBtnOnClick(View view) {
        if(!viewModel.validateOldPassword()) {
            ToastHelper.showToast("无效的密码");
        }
    }

    public void useCodeVerifyBtnOnClick(View view) {
        AccountBusinessPanelLayout.CodeVerify.addNext(AccountBusinessPanelLayout.ModifyPassword);
        AccountBusinessPanelLayout.SendVerifyCode.addNext(AccountBusinessPanelLayout.CodeVerify);
        viewModel.gotoPage(AccountBusinessPanelLayout.SendVerifyCode);
    }

    public void sendVerifyCodeBtnOnClick(View view) {
        viewModel.nextPage();
        viewModel.fetchVerificationCode();
    }

    public void fetchVerifyCodeBtnOnClick(View view) {
        viewModel.fetchVerificationCode();
    }

    /**验证码验证按钮事件响应*/
    public void verifyBtnOnClick(View view){
        viewModel.checkVerificationCode();
    }

    public void changePasswordBtnOnClick(View view){
        viewModel.updatePasswordAction();
    }

    public void verifyCodeOnFocusChange(View view, boolean hasFocus){
        if(hasFocus) {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_n);
        } else {
            view.setBackgroundResource(R.drawable.app_common_edit_verify_code_s);
        }
    }

    //解决光标在最前面的位置导致退格键失效的问题
    public boolean verifyCodeOnKey(View view, int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_DEL){
            EditText editText = (EditText) view;
            int textLength = editText.getText().toString().length();
            if(textLength>0) {
                editText.setSelection(textLength);
            }else{
                editText.setText("");
            }
        }
        return false;
    }

}
